package com.lw.extraceim.loader;

import android.content.Context;
import android.media.MediaRouter;

import androidx.annotation.NonNull;

import com.lw.extraceim.app.AppConstants;
import com.lw.extraceim.loader.base.BaseLoader;
import com.lw.extraceim.model.CustomerInfo;
import com.lw.extraceim.util.JsonUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author : liwei
 * @Email : 1759840027@qq.com
 * @Description : 客户信息的加载类
 * @Date : 2019/5/7 19:13
 */
public class CustomerLoader extends BaseLoader {
    private static final String TAG = "CustomerLoader";

    public CustomerLoader(Context context) {
        super(context);
    }

    /**
     * 添加客户信息
     *
     * @param customerInfo
     * @param callback
     */
    public void save(@NonNull CustomerInfo customerInfo, @NonNull Callback callback) {
        String url = AppConstants.CUSTOMER_SERVICE + "/add";
        String customerJson = JsonUtils.toJson(customerInfo);
        execute(url, customerJson, callback);
    }

    /**
     * 通过ID查找客户信息
     *
     * @param customerId
     * @param callback
     */
    public void get(int customerId, @NonNull Callback callback) {
        String url = AppConstants.CUSTOMER_SERVICE + "/get/" + customerId;
        execute(url, AppConstants.GET, null, callback);
    }

    /**
     * 通过姓名 电话号码 地区编码查找客户信息
     * @param name
     * @param phone
     * @param regionCode
     * @param callback
     */
    public void get(String name, String phone, String regionCode, @NonNull Callback callback) {
        String url = AppConstants.CUSTOMER_SERVICE + "/list";
        Map<String, String> form = new HashMap<>();
        if (name != null) {
            form.put("name", name);
        }
        if (phone != null) {
            form.put("phone", phone);
        }
        if (regionCode != null) {
            form.put("regionCode", regionCode);
        }
        execute(url, AppConstants.GET, form, callback);
    }

    /**
     * 修改用户信息
     * @param customerInfo
     * @param callback
     */
    public void update(@NonNull CustomerInfo customerInfo,@NonNull Callback callback){
        String url = AppConstants.CUSTOMER_SERVICE + "/update";
        String customerJson = JsonUtils.toJson(customerInfo);
        execute(url, customerJson, callback);
    }

}
